package com.bs.visitservice.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 上传工具类
 */
public class UploadUtil {
    /**
     * 图片转化为byte[]数组
     * @param file
     * @return
     */
    public static byte[] transfort(MultipartFile file){
        ByteArrayOutputStream bos  = new ByteArrayOutputStream();
        InputStream is = null;
        byte[] res = null;
        try {
             is = file.getInputStream();
             byte[] bytes = new byte[1024];
             int lenth = 0;
             while((lenth = is.read(bytes)) != -1){
                 bos.write(bytes, 0,lenth);
             }
            res = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(is != null){
                    is.close();
                }
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }
}
